package com.woniuxy.controller;

import com.woniuxy.entity.Register;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.RegisterService;
import com.woniuxy.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/register")
@Api(tags = "注册接口")
public class RegisterController {
    @Autowired
    RegisterService registerService;

    @PostMapping("/add")
    @ApiOperation(value = "新增用户")
    public ResponseEntity addOne(@ApiParam(name = "register", value="注册实体",required=true) @RequestBody Register register){
        //密码加盐
        String salt = MD5Util.generateSalt();
        register.setSalt(salt);
        String password = MD5Util.encryptPassword(register.getPassword(), salt);
        register.setPassword(password);
        //生成随机编码
        String s = UUID.randomUUID().toString().substring(0, 2);
        String no = "GR"+s;
        register.setNo(no);
        //默认角色为2-admin，权限为1-superAdmin
        register.setRoles("2");
        //状态默认开启
        register.setStatus("true");
        return registerService.addOne(register);
    }
    @GetMapping("/findByNo/{no}")
    @ApiOperation(value = "查找工号是否已存在")
    public ResponseEntity findByNo(@ApiParam(name = "no", value="工号",required=true) @PathVariable String no){
        return registerService.findByNo(no);
    }
    @GetMapping("/findByAccount/{account}")
    @ApiOperation(value = "查找昵称是否已存在")
    public ResponseEntity findByAccount(@ApiParam(name = "account", value="用户名",required=true) @PathVariable String account){
        return registerService.findByAccount(account);
    }
    @GetMapping("/findByUsername/{username}")
    @ApiOperation(value = "查找账号是否已存在")
    public ResponseEntity findByUsername(@ApiParam(name = "username", value="账号",required=true) @PathVariable String username){
        return registerService.findByUsername(username);
    }
    @GetMapping("/findAll")
    @ApiOperation(value = "查找所有数据")
    public ResponseEntity findAll(){
        return registerService.findAll();
    }
}
